package com.blrs.service.enterprise.info.impl;

import com.blrs.common.core.domain.entity.SysDept;
import com.blrs.common.core.domain.entity.SysDictData;
import com.blrs.common.core.domain.entity.SysUser;
import com.blrs.common.exception.ServiceException;
import com.blrs.common.utils.ReflectionUtils;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.domain.*;
import com.blrs.domain.vo.BusinessMainClassVo;
import com.blrs.mapper.*;
import com.blrs.service.enterprise.info.IEnterpriseInfoService;
import com.blrs.service.maintype.IMainTypeService;
import com.blrs.system.domain.SysUploadFile;
import com.blrs.system.domain.SysUserRole;
import com.blrs.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;

/**
 * 企业信息Service业务层处理
 * 
 * @author yuxiang.chen
 * @date 2023-08-08
 */
@Service
public class EnterpriseInfoServiceImpl implements IEnterpriseInfoService
{

    @Autowired
    private ElectricSalesInfoMapper electricSalesInfoMapper;
    @Autowired
    private SysUploadFileMapper sysUploadFileMapper;
    @Autowired
    private HighSeaRecordMapper highSeaRecordMapper;
    @Autowired
    private EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    private EnterpriseFollowRecordsMapper enterpriseFollowRecordsMapper;
    @Autowired
    private IMainTypeService mainTypeService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysAreaMapper sysAreaMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private EnterprisePersonInfoMapper enterprisePersonInfoMapper;

    @Autowired
    private ElectricSalesFollowRecordsMapper electricSalesFollowRecordsMapper;
    @Autowired
    private HighSeasMapper highSeasMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private FileCabinetInformationMapper fileCabinetInformationMapper;

    @Autowired
    private CollaborativeManagementMapper collaborativeManagementMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    @Autowired
    private EnterpriseSatisfiedMapper enterpriseSatisfiedMapper;
    /**
     * 查询企业信息
     *
     * @param enterpriseId 企业信息主键
     * @return 企业信息
     */
    @Override
    public EnterpriseInfo selectEnterpriseInfoByEnterpriseId(String enterpriseId)
    {
        List<EnterpriseInfo> dataList = enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseId1(enterpriseId);
        EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
        if(dataList.size()>0) {
            enterpriseInfo = enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseId(enterpriseId);
            EnterpriseFollowRecords record = new EnterpriseFollowRecords();
            record.setEnterpriseId(enterpriseInfo.getEnterpriseId());
            enterpriseInfo.setRecords(enterpriseFollowRecordsMapper.selectEnterpriseFollowRecordsList(record));
            //获取企业营业执照图片
            String s = sysUploadFileMapper.selectUrlByCode("enterprise", enterpriseInfo.getEnterpriseId());
            enterpriseInfo.setUrl(s);
            if (StringUtils.isNotNull(enterpriseInfo.getTransactionId())){
                enterpriseInfo.setBusinessManager(sysUserMapper.selectUserById(enterpriseInfo.getTransactionId()).getNickName());
            }
        }
        return enterpriseInfo;
    }

    //入驻企业审核
    @Override
    public int enterpriseVerify(SysDept dept) {
        int count;
        //审核通过修改角色 根据用户注册的企业类别修改所属角色端口管理员,并创建文件柜
        //企业类别：0:工业企业 1：服务机构 2 ：园区平台
        //角色端口:739:企业端 740:园区平台端 741:服务端
        if(dept.getIsCertified().equals("2")){//代表审核通过
            //文件柜维护
            for(int i=0;i<2;i++){
                FileCabinetInformation fileCabinetInformationAdd = new FileCabinetInformation();
                fileCabinetInformationAdd.setCabinetId(UUID.randomUUID().toString());
                fileCabinetInformationAdd.setSuperiorCabinetId("947cc995-011c-4577-81cc-8478ce3bab4b");
                if(i==0){
                    fileCabinetInformationAdd.setCabinetName("内部文件柜");
                    fileCabinetInformationAdd.setCabinetLocation("内部");
                }else{
                    fileCabinetInformationAdd.setCabinetName("外部文件柜");
                    fileCabinetInformationAdd.setCabinetLocation("外部");
                }
                fileCabinetInformationAdd.setSuperiorCabinetName("文件柜");
                fileCabinetInformationAdd.setCreationId(SecurityUtils.getUserId());
                fileCabinetInformationAdd.setCreationName(SecurityUtils.getUsername());
                fileCabinetInformationAdd.setCreationTime(new Timestamp(new Date().getTime()));
                fileCabinetInformationAdd.setOrganizationId(dept.getDeptId());
                fileCabinetInformationAdd.setOrganizationName(dept.getDeptName());
                fileCabinetInformationAdd.setDepartmentId(dept.getDeptId());
                fileCabinetInformationAdd.setOperateTime(new Timestamp(new Date().getTime()));
                fileCabinetInformationAdd.setOperateName(SecurityUtils.getUsername());
                fileCabinetInformationAdd.setOperateId(SecurityUtils.getUserId());
                fileCabinetInformationAdd.setBelongTo(dept.getDeptId());
                fileCabinetInformationAdd.setDepartmentName(dept.getDeptName());
                fileCabinetInformationAdd.setIdPath("947cc995-011c-4577-81cc-8478ce3bab4b,"+fileCabinetInformationAdd.getCabinetId());
                fileCabinetInformationMapper.insertFileCabinetInformation(fileCabinetInformationAdd);
            }

            //分配角色
            SysUser user = new SysUser();
            user.setDeptId(dept.getDeptId());
            user.setIsAdmin("1");
            //根机构id 查询用户
            List<SysUser> userList = sysUserMapper.selectUserList(user);
            //先删用户角色信息再新增
            userRoleMapper.deleteUserRoleByUserId(userList.get(0).getUserId());
            Long roleId = 0l;
            if(dept.getCompanyType().equals("0")){
                roleId = 739L;
            }else if(dept.getCompanyType().equals("1")){
                roleId = 741L;
            }else if(dept.getCompanyType().equals("2")){
                roleId = 740L;
            }
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            SysUserRole ur = new SysUserRole();
            ur.setUserId(userList.get(0).getUserId());
            ur.setRoleId(roleId);
            list.add(ur);
            userRoleMapper.batchUserRole(list);

            //修改客户信息 是否入驻修改为是 enterprise_org_id:deptId
            //先拿当前审核通过的企业名称去客户管理查询
            EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
            enterpriseInfo.setEnterpriseName(dept.getDeptName());
            List<EnterpriseInfo> enterpriseInfoList = selectEnterpriseInfoByEnterpriseName(enterpriseInfo);
            if(enterpriseInfoList.size()>0){
                for (EnterpriseInfo data : enterpriseInfoList) {
                    data.setEnterpriseOrgId(dept.getDeptId().toString());
                    enterpriseInfoMapper.updateEnterpriseInfo(data);
                }
            }

            dept.setIsCertified("2");
            count = sysDeptMapper.updateDept(dept);
        }else{
            dept.setIsCertified("3");
            count = sysDeptMapper.updateDept(dept);
        }
        return count;
    }
    /**
     * 根据企业名称判断是否存在于公海
     */
    @Override
    public List<Object> selectHighSeasByEnterpriseName(HighSeas data) {
        //先判断是否已存在与客户中
        Long number = enterpriseInfoMapper.number(data.getEnterpriseName(), String.valueOf(SecurityUtils.getOrgId()));
        if (number > 0) {
            throw new ServiceException("企业已存在!");
        } else {
            //查询电销信息
            ElectricSalesInfo electricSalesInfo = new ElectricSalesInfo();
            electricSalesInfo.setEnterpriseName(data.getEnterpriseName());
            electricSalesInfo.setOrganizationId(SecurityUtils.getOrgId());
            List<ElectricSalesInfo> electricSalesInfos = electricSalesInfoMapper.selectElectricSalesInfoListOrgId(electricSalesInfo);
            data.setBelongTo(SecurityUtils.getOrgId());
            List<HighSeas> highseas = enterpriseInfoMapper.selectHighSeasByEnterpriseName(data);
            if (!electricSalesInfos.isEmpty()) {
                ElectricSalesInfo salesInfo = electricSalesInfos.get(0);
                salesInfo.setElectricFlag("1");
                return new ArrayList<>(Arrays.asList(salesInfo)); // 修改这里，将对象放入列表中
            } else {
                if (!highseas.isEmpty()){
                    if (Objects.nonNull(highseas.get(0).getDevelopManager())) {
                        highseas.get(0).setDevelopmentName(sysUserMapper.selectUserById(highseas.get(0).getDevelopManager()).getUserName());
                    }
                    if (Objects.nonNull(highseas.get(0).getBusinessManager())) {
                        highseas.get(0).setBusinessManagerName(sysUserMapper.selectUserById(highseas.get(0).getBusinessManager()).getUserName());
                    }
                    HighSeas seas = highseas.get(0);
                    seas.setHighSeasFlag("2");
                    return new ArrayList<>(Arrays.asList(seas));
                }else {
                    return new ArrayList<>();
                }
            }
        }
    }


    /**
     * 根据企业名称判断是否存在于电销
     */
    @Override
    public List<ElectricSalesInfo> selectSaleByEnterpriseName(ElectricSalesInfo data)
    {
        List<ElectricSalesInfo> sale  = enterpriseInfoMapper.selectSaleByEnterpriseName(data);
        if(sale.size()>0){
            if (Objects.nonNull(sale.get(0).getDevelopmentId())){
                sale.get(0).setDevelopManager(sysUserMapper.selectUserById(sale.get(0).getDevelopmentId()).getUserName());
            }
            if (Objects.nonNull(sale.get(0).getTransactionId())){
                sale.get(0).setBusinessManager(sysUserMapper.selectUserById(sale.get(0).getTransactionId()).getUserName());
            }
        }
        return sale;
    }
    @Override
    public List<EnterpriseInfo> selectEnterpriseInfoByEnterpriseName(EnterpriseInfo enterpriseInfo)
    {
        return enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseName(enterpriseInfo);
    }
    @Override
    public String getMaxEnterpriseNo(String belongTo)
    {
        return enterpriseInfoMapper.getMaxEnterpriseNo(belongTo);
    }
    @Override
    public List<EnterpriseInfo> getNewFollowList(EnterpriseInfo enterpriseInfo){
        Long userid = SecurityUtils.getUserId();

        if(userid!=1&&userid!=2){
            enterpriseInfo.setBelongTo(SecurityUtils.getOrgId().toString());
            //fileCabinetInformation.setBelongTo(SecurityUtils.getOrgId());
        }
        List<EnterpriseInfo> list = enterpriseInfoMapper.getNewFollowList(enterpriseInfo);
        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                if(!StringUtils.isEmpty(list.get(i).getBusinessType())) {
                    if (list.get(i).getBusinessType().equals("1")) {
                        if (!list.get(i).getTitle().equals("企业信息")) {
                            list.get(i).setLatestDevelopments("新增" + list.get(i).getTitle());
                        } else {
                            list.get(i).setFollowTime(null);
                        }
                    } else if (list.get(i).getBusinessType().equals("2")) {
                        if (!list.get(i).getTitle().equals("企业信息")) {
                            list.get(i).setLatestDevelopments("更新" + list.get(i).getTitle());
                        } else {
                            list.get(i).setLatestDevelopments("更新" + list.get(i).getTitle());
                        }

                    }
                }

            }
        }
        return list;
    }

    @Override
    public List<BusinessOpportunityInformation> getNewBusinessFollowList(BusinessOpportunityInformation data){
        Long userid = SecurityUtils.getUserId();
        if(userid!=1){
            data.setBelongTo((SecurityUtils.getOrgId()));
        }
        List<BusinessOpportunityInformation> list = enterpriseInfoMapper.getNewBusinessFollowList(data);
        return list;
    }


    /**
     * 查询企业信息列表
     *
     * @param enterpriseInfo 企业信息
     * @return 企业信息
     */
    @Override
    public List<EnterpriseInfo> selectEnterpriseInfoList(EnterpriseInfo enterpriseInfo)
    {
        Long userid = SecurityUtils.getUserId();
        System.out.println(SecurityUtils.getOrgId());
        /*//获取当前登录用户的角色 超管或平台管理员 查看所有数据
        SysUserRole uerRole = new SysUserRole();
        uerRole.setUserId(userid);
        List<SysUserRole> userRoleList = sysUserRoleMapper.selectUserRoleList(uerRole);
        if(userRoleList.size()>0){//1=超管 113=平台管理员
            if(userRoleList.get(0).getRoleId()!=1||userRoleList.get(0).getRoleId()!=113){
                enterpriseInfo.setBelongTo(SecurityUtils.getOrgId());
                System.out.println("客户管理的组织机构id"+enterpriseInfo.getBelongTo());
            }
        }*/
        if (StringUtils.isNull(enterpriseInfo.getBelongTo())){
            if(userid!=1){
                enterpriseInfo.setBelongTo(SecurityUtils.getOrgId().toString());
                System.out.println("客户管理的组织机构id"+enterpriseInfo.getBelongTo());
            }
        }
        if (SecurityUtils.getLoginUser().getUser().getIsAdmin().equals("1")){
            enterpriseInfo.setTransactionId(null);
        }
        CollaborativeManagement collaborativeManagement = new CollaborativeManagement();
        collaborativeManagement.setOrgId(SecurityUtils.getOrgId().toString());
        //判断行政区域是否为空
        if(StringUtils.isNotNull(enterpriseInfo.getAdministrativeArea())) {
            //根据行政区域查询子区域
            List<String> areaList = sysAreaMapper.selectSysAreaListId(enterpriseInfo.getAdministrativeArea());
            enterpriseInfo.setAreaList(areaList);
        }
            //根据行政区域查询子区域
        //这里需要判断企业信息列表中有哪些是入驻的，哪些不是入驻的
        List<EnterpriseInfo> list = enterpriseInfoMapper.selectEnterpriseInfoList(enterpriseInfo);
        for (EnterpriseInfo info : list) {
            info.setEnterpriseNo(info.getEnterpriseNo());
            if (Objects.nonNull(sysDeptMapper.selectByDeptName(info.getEnterpriseName()))){
                info.setSettle(true);
            }else {
                info.setSettle(false);
            }
            if (StringUtils.isNotNull(info.getTransactionId())){
                info.setBusinessManager(sysUserMapper.selectUserById(info.getTransactionId()).getNickName());
            }
            if(StringUtils.isNotNull(info.getDevelopmentId())){
                info.setDevelopmentManager(sysUserMapper.selectUserById(info.getDevelopmentId()).getNickName());
            }
            collaborativeManagement.setRecipientName(info.getEnterpriseName());
            collaborativeManagement.setInitiatorName(info.getEnterpriseName());
           Long collaborativeCount=collaborativeManagementMapper.selectCollaborativeManagementCount(collaborativeManagement);
//           //满意度最新情况
//            EnterpriseSatisfied enterpriseSatisfied=enterpriseSatisfiedMapper.selectEnterpriseSatisfiedByEnterpriseId(info.getEnterpriseId());
//           if (Objects.nonNull(enterpriseSatisfied)){
//               info.setSatisfactionTime(enterpriseSatisfied.getCreateTime());
//               if (enterpriseSatisfied.getCorrection()!=null){
//                   info.setSatisfaction(enterpriseSatisfied.getCorrection());
//               }
//           }
           if (collaborativeCount>0){
               //说明当前企业存在合作，注册状态为已注册
               info.setRegisterStatus(1);
           }else {
               //说明当前企业不存在合作，注册状态为未注册
               info.setRegisterStatus(0);
           }
            //根据项目经理和业务经理  2024/1/14  韩总要求修改最新进展只展示最新进展的最新内容
            if(!StringUtils.isEmpty(info.getBusinessType())){
                if(info.getBusinessType().equals("1")){
                    if(info.getTitle().equals("跟进记录")){
                        info.setLatestDevelopments("新增"+info.getTitle());
                    }else{
                        info.setFollowTime(null);
                    }
                }else if(info.getBusinessType().equals("2")){
                    if(info.getTitle().equals("跟进记录")){
                        info.setLatestDevelopments("更新"+info.getTitle());
                    }else{
                        info.setFollowTime(null);
                    }

                }/*else if(list.get(i).getBusinessType().equals("5")){
                    list.get(i).setLatestDevelopments("导出了"+list.get(i).getTitle());
                }else if(list.get(i).getBusinessType().equals("6")){
                    list.get(i).setLatestDevelopments("导入了"+list.get(i).getTitle());
                }*/
            }
            /*else{
                info.setLatestDevelopments("导入企业信息");
            }*/
        }
        /*for(int i=0;i<list.size();i++){


        }*/

        return list;
    }
    @Override
    public List<EnterpriseInfo> selectEnterpriseInfoListPrivate(EnterpriseInfo enterpriseInfo)
    {
        Long userid = SecurityUtils.getUserId();
        System.out.println(SecurityUtils.getOrgId());
        if(userid!=1){
            enterpriseInfo.setBelongTo(SecurityUtils.getOrgId().toString());

            System.out.println("客户管理的组织机构id"+enterpriseInfo.getBelongTo());
        }
        //这里需要判断企业信息列表中有哪些是入驻的，哪些不是入驻的
        List<EnterpriseInfo> list = enterpriseInfoMapper.selectEnterpriseInfoListPrivate(enterpriseInfo);
        for (EnterpriseInfo info : list) {
            info.setEnterpriseNo(info.getEnterpriseNo());
            if (Objects.nonNull(sysDeptMapper.selectByDeptName(info.getEnterpriseName()))){
                info.setSettle(true);
            }else {
                info.setSettle(false);
            }
            if(!StringUtils.isEmpty(info.getBusinessType())){
                if(info.getBusinessType().equals("1")){
                    if(!info.getTitle().equals("企业信息")){
                        info.setLatestDevelopments("新增"+info.getTitle());
                    }else{
                        info.setFollowTime(null);
                    }
                }else if(info.getBusinessType().equals("2")){
                    if(!info.getTitle().equals("企业信息")){
                        info.setLatestDevelopments("更新"+info.getTitle());
                    }else{
                        info.setLatestDevelopments("更新"+info.getTitle());
                    }

                }/*else if(list.get(i).getBusinessType().equals("5")){
                    list.get(i).setLatestDevelopments("导出了"+list.get(i).getTitle());
                }else if(list.get(i).getBusinessType().equals("6")){
                    list.get(i).setLatestDevelopments("导入了"+list.get(i).getTitle());
                }*/
            }/*else{
                info.setLatestDevelopments("导入企业信息");
            }*/
        }


        return list;
    }
    @Override
    public List<EnterpriseInfo> selectEnterpriseInfoListToAdmin(EnterpriseInfo enterpriseInfo)
    {
        List<EnterpriseInfo> list = enterpriseInfoMapper.selectEnterpriseInfoListToAdmin(enterpriseInfo);
        return list;
    }
    @Override
    public List<EnterpriseInfo> selectEnterpriseInfoByEnterpriseId1(String enterpriseId)
    {
        return enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseId1(enterpriseId);
    }
    /**
     * 新增企业信息
     *
     * @param enterpriseInfo 企业信息
     * @return 结果
     */
    @Override
    public int insertEnterpriseInfo(EnterpriseInfo enterpriseInfo)
    {

        return enterpriseInfoMapper.insertEnterpriseInfo(enterpriseInfo);
    }
    /**
     * 导入企业信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importExcel(List<EnterpriseInfo> list,String newMarkAs) throws NoSuchFieldException, IllegalAccessException {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入客户信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        int personAddNum = 0;
        int personUpdateNum= 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (EnterpriseInfo data : list) {
            try {
                Integer numEnterpriseNo = Integer.valueOf(data.getEnterpriseNo());
                if (StringUtils.isEmpty(data.getEnterpriseName())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、企业名称为空！ ");
                }
//                else if (StringUtils.isEmpty(data.getEnterpriseAddress())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、详细地址为空！ ");

//                }else if (StringUtils.isEmpty(data.getAreaName())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、行政区域为空！ ");
//                }else if (StringUtils.isEmpty(data.getEnterpriseNature())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、企业性质为空！ ");
//                }else if (StringUtils.isEmpty(data.getEnterpriseSource())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、企业来源为空！ ");
//                }else if (StringUtils.isEmpty(data.getIndustry())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、所属行业为空！ ");
//                }else if (StringUtils.isEmpty(data.getStatus())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、状态为空！ ");
//                }else if (data.getEnterpriseNo()==null) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、公司编号为空或类型错误，公司编号只能填写纯数字！ ");
//                }else if (StringUtils.isEmpty(data.getContacts())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、联系人姓名为空！ ");
//                }
//            else if (StringUtils.isEmpty(data.getTelephone())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、联系人电话为空！ ");
//                }else if (StringUtils.isEmpty(data.getDevelopmentManager())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、开发经理为空！ ");
//                }else if (StringUtils.isEmpty(data.getBusinessManager())) {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、业务经理为空！ ");
//                }else if(StringUtils.isEmpty(data.getDevelopmentManager())){
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、开发经理不能为空 ");
//                }else if(StringUtils.isNotEmpty(data.getBusinessManager())&&getUserIdByName(data.getBusinessManager())==null){
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、业务经理不是该公司用户！ ");
//                }
                String noo = enterpriseInfoMapper.getMaxEnterpriseNo(String.valueOf(SecurityUtils.getOrgId()));
                if(!StringUtils.isNotEmpty(noo)){
                    noo ="1";
                }else{
                    noo = enterpriseInfoMapper.getMaxEnterpriseNo(String.valueOf(SecurityUtils.getOrgId()));
                }
                Long countNo = enterpriseInfoMapper.getEnterpriseNoCount(data.getEnterpriseNo(),Integer.parseInt(SecurityUtils.getOrgId().toString()));
                if(countNo>0) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、公司编号已存在，请按编号顺序重新输入！当前公司编号为: "+noo);
                }

            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
             //客户信息
            StringBuilder enterpriseBuilder = new StringBuilder();
            //公海信息
            StringBuilder highSeasBuilder = new StringBuilder();
            //电销信息
            StringBuilder electricBuilder = new StringBuilder();
            //新增人员信息
            for (EnterpriseInfo data : list) {
                Long number=enterpriseInfoMapper.number(data.getEnterpriseName(),String.valueOf(SecurityUtils.getOrgId()));
                Long numberHigh=enterpriseInfoMapper.numberHighByEnterpriseName(data.getEnterpriseName(),String.valueOf(SecurityUtils.getOrgId()));
                Long numberElectric=enterpriseInfoMapper.numberElectricByEnterpriseName(data.getEnterpriseName(),String.valueOf(SecurityUtils.getOrgId()));
//                if (number>0){
//                    throw new ServiceException(data.getEnterpriseName()+":企业已存在!");
//                }else if(numberElectric>0){
//                    throw new ServiceException(data.getEnterpriseName()+":企业数据已存在，转入指定电销人员处理!");
//                }else if(numberHigh>0){
//                    throw new ServiceException(data.getEnterpriseName()+":企业在公海中存在!");
//                }
                if (number>0){
//                    enterpriseBuilder.append(data.getEnterpriseName()).append("<br>");
                    continue;
                }else if(numberElectric>0){
//                    electricBuilder.append(data.getEnterpriseName()).append("<br>");
                    continue;
                }else if(numberHigh>0){
//                    highSeasBuilder.append(data.getEnterpriseName()).append("<br>");
                    continue;
                }
                if(number==0&&numberHigh==0&&numberElectric==0){
//                    throw new ServiceException(data.getEnterpriseName()+":企业已存在!");
//                }else if(numberHigh>0){
//                    throw new ServiceException(data.getEnterpriseName()+":企业在公海中存在!");
//                }else if(numberElectric>0){
//                    throw new ServiceException(data.getEnterpriseName()+":企业在电销中存在!");
//                }else{}
                    /*String no = getMaxEnterpriseNo(SecurityUtils.getOrgId().toString());
                    int noo = 1;
                    if(!StringUtils.isEmpty(no)){
                        noo += no+1;
                    }*/
                    //判断是否有选择所属行业
                    if(StringUtils.isEmpty(data.getIndustry())){
                        data.setIndustryType("0");
                    }else{
                        //拿所属行业去查字典 查询此行业所属类别
                        SysDictData sysDictData = new SysDictData();
                        sysDictData.setDictType("customer_industry_all");
                        sysDictData.setDictValue(data.getIndustry());
                        List<SysDictData> sysDictDataList = dictDataMapper.selectDictDataList(sysDictData);

                        String dictLabel = sysDictDataList.get(0).getDictLabel();
                        List<SysDictData> sysDictDataLabelList = dictDataMapper.selectDictDataByLabel(dictLabel);
                        //根据字典类型判断行业类别
                        if(sysDictDataLabelList.get(0).getDictType().equals("customer_industry")){
                            data.setIndustryType("0");
                        }else if(sysDictDataLabelList.get(0).getDictType().equals("customer_industry_serve")){
                            data.setIndustryType("1");
                        }else{
                            data.setIndustryType("2");
                        }
                    }

                    data.setEnterpriseNo(data.getEnterpriseNo());
                    data.setAdministrativeArea(sysAreaMapper.selectSysAreaByAreaName(data.getAreaName()));
                    data.setMarkAs("1");
                    data.setEnterpriseOrgId("0");
                    data.setIsShare("0");
                    data.setEnterpriseId(UUID.randomUUID().toString());
                    SysUser userDevelop = new SysUser();
                    userDevelop.setNickName(data.getDevelopmentManager());
                    userDevelop.setBelongTo(SecurityUtils.getOrgId());
                    /*SysUser userTransaction = new SysUser();
                    userTransaction.setNickName(data.getBusinessManager());
                    userTransaction.setBelongTo(SecurityUtils.getOrgId());*/
                    data.setDevelopmentId(getUserIdByName(data.getDevelopmentManager()));//开发经理
                    data.setTransactionId(getUserIdByName(data.getBusinessManager()));//业务经理
                    data.setClassificationId("650c44e1-b575-4b62-9d54-1ba9d1e8abb7");
                    data.setCreationId(SecurityUtils.getUserId().toString());
                    data.setCreationName(SecurityUtils.getUsername());
                    data.setCreationTime(new Timestamp(new Date().getTime()));
                    data.setOrganizationId(SecurityUtils.getOrgId());
                    data.setOrganizationName(SecurityUtils.getOrgName());
                    data.setDepartmentId(SecurityUtils.getDeptId());
                    data.setOperateTime(new Timestamp(new Date().getTime()));
                    data.setOperateName(SecurityUtils.getUsername());
                    data.setOperateId(SecurityUtils.getUserId());
                    data.setDelFlag("0");
                    data.setBelongTo(SecurityUtils.getOrgId().toString());
                    data.setDepartmentName(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
                    //data.setIdPath(mainTypeService.selectMainTypeById(data.getClassificationId()).getIdPath());
                    ReflectionUtils.setCommonPrivateProperty(data);
                    enterpriseInfoMapper.insertEnterpriseInfo(data);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、客户信息数据导入成功");

                    if (StringUtils.isNotEmpty(data.getContacts())){
                        //新增人员信息 根据企业名称和电话号码去查询
                        EnterprisePersonInfo personInfo = new EnterprisePersonInfo();
                        personInfo.setPhone(data.getTelephone());
                        personInfo.setOrganizationId(SecurityUtils.getOrgId());
                        personInfo.setEnterpriseName(data.getEnterpriseName());
                        personInfo.setEnterpriseId(data.getEnterpriseId());
                        List<EnterprisePersonInfo> personInfoList = enterprisePersonInfoMapper.selectEnterprisePersonInfoListByPhone(personInfo);
                        if (personInfoList.size() == 0) {
                            personInfo.setId(UUID.randomUUID().toString());
                            personInfo.setPhone(data.getTelephone());
                            personInfo.setName(data.getContacts());
                            personInfo.setEnterpriseId(data.getEnterpriseId());
                            personInfo.setEnterpriseName(data.getEnterpriseName());
                            personInfo.setCreateId(SecurityUtils.getUserId());
                            personInfo.setOrganizationId(SecurityUtils.getOrgId());
                            personInfo.setOrganizationName(SecurityUtils.getOrgName());
                            personInfo.setClassificationId("650c44e1-b575-4b62-9d54-1ba9d1e8abb7");
                            personInfo.setFilingTime(new Timestamp(new Date().getTime()));
                            personInfo.setCreationTime(new Timestamp(new Date().getTime()));
                            personInfo.setOperateTime(new Timestamp(new Date().getTime()));
                            personInfo.setDelFlag(0);
                            enterprisePersonInfoMapper.insertEnterprisePersonInfo(personInfo);
                            personAddNum++;
                            successMsg.append("，人员信息新增成功");
                        } else {
                            EnterprisePersonInfo enterprisePersonInfo = enterprisePersonInfoMapper.selectEnterprisePersonInfoById(personInfoList.get(0).getId());
                            enterprisePersonInfo.setName(data.getContacts());
                            enterprisePersonInfo.setEnterpriseId(data.getEnterpriseId());
                            enterprisePersonInfo.setEnterpriseName(data.getEnterpriseName());
                            enterprisePersonInfo.setOrganizationId(SecurityUtils.getOrgId());
                            enterprisePersonInfo.setOrganizationName(SecurityUtils.getOrgName());
                            enterprisePersonInfo.setOperateTime(new Timestamp(new Date().getTime()));
                            enterprisePersonInfoMapper.updateEnterprisePersonInfo(enterprisePersonInfo);
                            personUpdateNum++;
                            successMsg.append("，人员信息更新成功");
                        }
                    }
                }
            }
//            if (enterpriseBuilder.length() > 0){
//                //去除最后一个逗号
//                enterpriseBuilder.deleteCharAt(enterpriseBuilder.length() - 1);
//                throw new ServiceException("以下企业数据已存在或导入文档中重复，请勿重复导入：<br>"+enterpriseBuilder.toString());
//            }else if (electricBuilder.length() > 0){
//                electricBuilder.deleteCharAt(electricBuilder.length() - 1);
//                throw new ServiceException("以下企业已分配到指定的电销人员账号中，请勿重复导入：<br>"+electricBuilder.toString());
//            }else if (highSeasBuilder.length() > 0){
//                highSeasBuilder.deleteCharAt(highSeasBuilder.length() - 1);
//                 throw new ServiceException("以下企业在公海中已存在，请勿重复导入：<br>"+highSeasBuilder.toString());
//            }else {
//
//            }
            successMsg.insert(0, "恭喜您，导入客户数据 " + successNum + " 条，新增人员数据"+personAddNum+"条，更新人员数据"+personUpdateNum+"条，数据如下：");
        }

        return successMsg.toString();
    }
    //删除公海里面的客户数据
    @Override
    public int deleteHighSeasByEnterpriseId(String enterpriseId)
    {
        //查询公海进展记录
        return enterpriseInfoMapper.deleteHighSeasByEnterpriseId(enterpriseId);
    }
    private Long getUserIdByName(String name){
        SysUser user = sysUserMapper.selectUserByUserName(name);
        if(user!=null){
            return user.getUserId();
        }else{
            return null;
        }
    }

//    private Long getUserByNickName(SysUser user){
//        SysUser userData = sysUserMapper.selectUserByNickName(user);
//        if(userData!=null){
//            return userData.getUserId();
//        }else{
//            return null;
//        }
//    }
    /**
     * 修改企业信息
     *
     * @param enterpriseInfo 企业信息
     * @return 结果
     */
    @Override
    public int updateEnterpriseInfo(EnterpriseInfo enterpriseInfo)
    {
        //EnterpriseLabel 暂用于区分数据类型 1=电销 2=公海
        //enterpriseAbbreviation 暂用于存电销 公海主键id
        //markAsName 暂用于区分当前数据标记为
        String markas;
        if(StringUtils.isNotEmpty(enterpriseInfo.getEnterpriseLabel())){
            EnterpriseFollowRecords enterpriseFollowRecords = new EnterpriseFollowRecords();
            if(enterpriseInfo.getEnterpriseLabel().equals("1")){//存在于电销
                //查询电销管理记录
                ElectricSalesInfo esi = electricSalesInfoMapper.selectElectricSalesInfoByElectricId(enterpriseInfo.getEnterpriseId());
                //更改附件
                SysUploadFile file=new SysUploadFile();
                file.setBusinessId(esi.getBusinessLicense());
                List<SysUploadFile> listFile = sysUploadFileMapper.selectSysUploadFileList(file);//业务数据附件
                if(listFile.size()>0){
                    SysUploadFile sysUploadFileNew=new SysUploadFile();
                    sysUploadFileNew.setId(listFile.get(0).getId());
                    sysUploadFileNew.setBusinessId(enterpriseInfo.getEnterpriseId());
                    sysUploadFileNew.setBusinessCode("enterprise");
                    sysUploadFileMapper.updateSysUploadFile(sysUploadFileNew);
                }
                //给客户管理表添加电销id
                enterpriseInfo.setEnterpriseId(esi.getElectricId());
                //删除电销管理记录信息
                electricSalesInfoMapper.deleteElectricSalesInfoByElectricId(esi.getElectricId());
                //删除公海管理记录信息
                deleteHighSeasByEnterpriseId(enterpriseInfo.getEnterpriseId());
                //查询电销进展记录
                ElectricSalesFollowRecords e1 = new ElectricSalesFollowRecords();
                e1.setElectricId(enterpriseInfo.getEnterpriseAbbreviation());
                //SysUser user = new SysUser();
                List<ElectricSalesFollowRecords> list = electricSalesFollowRecordsMapper.selectElectricSalesFollowRecordsList(e1);
//                if(list.size()>0){
//                    for(int i = 0; i < list.size(); i++){
//                        enterpriseFollowRecords.setRecordsId(UUID.randomUUID().toString());
//                        enterpriseFollowRecords.setDelFlag("0");
//                        enterpriseFollowRecords.setEnterpriseId(enterpriseInfo.getEnterpriseId());
//                        enterpriseFollowRecords.setLatestDevelopments(list.get(i).getLatestDevelopments());
//                        enterpriseFollowRecords.setAnnotationsName(list.get(i).getAnnotationsName());
//                        enterpriseFollowRecords.setAnnotations(list.get(i).getAnnotations());
//                       /* user.setNickName(list.get(i).getAnnotationsName());
//                        user.setBelongTo(SecurityUtils.getOrgId());*/
//                        enterpriseFollowRecords.setAnnotationsId(getUserIdByName(list.get(i).getAnnotationsName()));;
//                        enterpriseFollowRecords.setType(list.get(i).getType());
//                        enterpriseFollowRecords.setFollowId(list.get(i).getFollowId());
//                        enterpriseFollowRecords.setFollowPerson(list.get(i).getFollowPerson());
//                        enterpriseFollowRecords.setFollowTime(list.get(i).getFollowTime());
//                        enterpriseFollowRecordsMapper.insertEnterpriseFollowRecords(enterpriseFollowRecords);
//                        //enterpriseFollowRecords.setAnnex(list.get(i).getAnnex());
//                        //修改电销记录的附件
//                        SysUploadFile sysUploadFile=new SysUploadFile();
//                        sysUploadFile.setBusinessId(list.get(i).getAnnex());
//                        List<SysUploadFile> listUploadFile = sysUploadFileMapper.selectSysUploadFileList(sysUploadFile);//进展附件
//                        if(listUploadFile.size()>0){
//                            SysUploadFile sysUploadFileNew=new SysUploadFile();
//                            sysUploadFileNew.setId(listUploadFile.get(0).getId());
//                            sysUploadFileNew.setBusinessId(enterpriseFollowRecords.getRecordsId());
//                            sysUploadFileNew.setBusinessCode("enterprise_records");
//                            sysUploadFileMapper.updateSysUploadFile(sysUploadFileNew);
//                        }
//                        //查询批注附件
//                        SysUploadFile sysUploadFileAnnotatio=new SysUploadFile();
//                        sysUploadFileAnnotatio.setBusinessId(list.get(i).getAnnexAn());
//                        List<SysUploadFile> listUploadFileAnnotatio = sysUploadFileMapper.selectSysUploadFileList(sysUploadFileAnnotatio);//批注附件
//                        if(listUploadFileAnnotatio.size()>0){
//                            SysUploadFile sysUploadFileNew=new SysUploadFile();
//                            sysUploadFileNew.setId(listUploadFileAnnotatio.get(0).getId());
//                            sysUploadFileNew.setBusinessId(enterpriseFollowRecords.getRecordsId());
//                            sysUploadFileNew.setBusinessCode("enterprise_annotatio");
//                            sysUploadFileMapper.updateSysUploadFile(sysUploadFileNew);
//                        }
//                    }
//                    //删除电销进展记录
//                    electricSalesFollowRecordsMapper.deleteElectricSalesFollowRecordsByRecordsId(enterpriseInfo.getEnterpriseAbbreviation());
//                }
            }else{//只存在于公海
                //查询公海管理记录
                HighSeas highSeas = highSeasMapper.selectHighSeasByEnterpriseId(enterpriseInfo.getEnterpriseId());
                //更改附件
                SysUploadFile file=new SysUploadFile();
                file.setBusinessId(highSeas.getBusinessLicense());
                List<SysUploadFile> listFile = sysUploadFileMapper.selectSysUploadFileList(file);//业务数据附件
                if(listFile.size()>0){
                    SysUploadFile sysUploadFileNew=new SysUploadFile();
                    sysUploadFileNew.setId(listFile.get(0).getId());
                    sysUploadFileNew.setBusinessId(enterpriseFollowRecords.getRecordsId());
                    sysUploadFileNew.setBusinessCode("enterprise");
                    sysUploadFileMapper.updateSysUploadFile(sysUploadFileNew);
                }
                //给客户管理表添加公海id
                enterpriseInfo.setEnterpriseId(highSeas.getEnterpriseId());
                //删除公海管理记录信息
                deleteHighSeasByEnterpriseId(highSeas.getEnterpriseId());
                //删除电销管理记录信息
                electricSalesInfoMapper.deleteElectricSalesInfoByElectricId(enterpriseInfo.getEnterpriseId());
                //查询公海进展记录
                //公海进展记录添加 先查询
                HighSeaRecord d1 = new HighSeaRecord();
                d1.setSeaId(enterpriseInfo.getEnterpriseAbbreviation());
                //查询公海进展记录
                List<HighSeaRecord> list = highSeaRecordMapper.selectHighSeaRecordList(d1);
            }
        }

        if(enterpriseInfo.getEnterpriseId()==null||enterpriseInfo.getEnterpriseId().replaceAll(" ","")==""){
            throw new ServiceException("系统错误！请刷新页面！");
        }
        //操作人员信息 根据客户id
        EnterprisePersonInfo personInfo = new EnterprisePersonInfo();
        personInfo.setPhone(enterpriseInfo.getTelephone());
        personInfo.setOrganizationId(SecurityUtils.getOrgId());
        personInfo.setEnterpriseName(enterpriseInfo.getEnterpriseName());
        personInfo.setEnterpriseId(enterpriseInfo.getEnterpriseId());
        List<EnterprisePersonInfo> personInfoList = enterprisePersonInfoMapper.selectEnterprisePersonInfoListByPhone(personInfo);
        if (personInfoList.size() == 0) {
            personInfo.setId(UUID.randomUUID().toString());
            personInfo.setName(enterpriseInfo.getContacts());
            personInfo.setEnterpriseId(enterpriseInfo.getEnterpriseId());
            personInfo.setEnterpriseName(enterpriseInfo.getEnterpriseName());
            personInfo.setCreateId(SecurityUtils.getUserId());
            personInfo.setOrganizationId(SecurityUtils.getOrgId());
            personInfo.setOrganizationName(SecurityUtils.getOrgName());
            personInfo.setDepartmentId(SecurityUtils.getDeptId());
            personInfo.setDepartmentName(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
            personInfo.setClassificationId("650c44e1-b575-4b62-9d54-1ba9d1e8abb7");
            personInfo.setFilingTime(new Timestamp(new Date().getTime()));
            personInfo.setCreationTime(new Timestamp(new Date().getTime()));
            personInfo.setOperateTime(new Timestamp(new Date().getTime()));
            personInfo.setDelFlag(0);
            enterprisePersonInfoMapper.insertEnterprisePersonInfo(personInfo);
        } else {
            EnterprisePersonInfo enterprisePersonInfo = enterprisePersonInfoMapper.selectEnterprisePersonInfoById(personInfoList.get(0).getId());
            enterprisePersonInfo.setName(enterpriseInfo.getContacts());
            enterprisePersonInfo.setEnterpriseId(enterpriseInfo.getEnterpriseId());
            enterprisePersonInfo.setEnterpriseName(enterpriseInfo.getEnterpriseName());
            enterprisePersonInfo.setOrganizationId(SecurityUtils.getOrgId());
            enterprisePersonInfo.setOrganizationName(SecurityUtils.getOrgName());
            enterprisePersonInfo.setFilingTime(new Timestamp(new Date().getTime()));
            enterprisePersonInfo.setCreationTime(new Timestamp(new Date().getTime()));
            enterprisePersonInfo.setOperateTime(new Timestamp(new Date().getTime()));
            enterprisePersonInfoMapper.updateEnterprisePersonInfo(enterprisePersonInfo);
        }
       Long count=enterpriseInfoMapper.count(enterpriseInfo.getEnterpriseId());
       //企业名称去重
       if(count>0){
           EnterpriseInfo info=this.selectEnterpriseInfoByEnterpriseId(enterpriseInfo.getEnterpriseId());
           if(!info.getEnterpriseName().equals(enterpriseInfo.getEnterpriseName())){
               Long number=enterpriseInfoMapper.number(enterpriseInfo.getEnterpriseName(),String.valueOf(SecurityUtils.getOrgId()));
               if(number>0){
                   throw new ServiceException("企业已存在!");
               }
           }
        }else{
           Long number=enterpriseInfoMapper.number(enterpriseInfo.getEnterpriseName(),String.valueOf(SecurityUtils.getOrgId()));
           if(number>0){
               throw new ServiceException("企业已存在!");
           }
       }
      /* String[] nae = enterpriseInfo.getMarkAs().split(",");

       String markAsName="";
       int counta = 0;
       for(int i=0;i<nae.length;i++){
           counta++;
           if(nae[i].equals("1")){
               markAsName+="客户";
           }else if(nae[i].equals("2")){
               markAsName+="供应商";
           }else if(nae[i].equals("3")){
               markAsName+="渠道";
           }
           if(counta<nae.length){
               markAsName+=",";
           }
       }*/
       enterpriseInfo.setMarkAsName("客户");
       if(count==0){
           enterpriseInfo.setCreationId(SecurityUtils.getUserId().toString());
           enterpriseInfo.setCreationName(SecurityUtils.getUsername());
           enterpriseInfo.setCreationTime(new Timestamp(new Date().getTime()));

       }else{
           enterpriseInfo.setOperateId(SecurityUtils.getUserId());
           enterpriseInfo.setOperateName(SecurityUtils.getUsername());
           enterpriseInfo.setOperateTime(new Timestamp(new Date().getTime()));
           //enterpriseInfo.setCreationTime(enterpriseInfo.getCreationTime());
       }
       //判断当前客户是否已入住
        SysDept dept =  new SysDept();
        dept.setDeptName(enterpriseInfo.getEnterpriseName());
        dept.setIsCertified("2");
        SysDept deptDate = sysDeptMapper.selectDeptByDeptName(dept);
        if(StringUtils.isNotNull(deptDate)){
            enterpriseInfo.setEnterpriseOrgId(deptDate.getDeptId().toString());
            enterpriseInfoMapper.updateEnterpriseInfo(enterpriseInfo);
        }else {
            enterpriseInfo.setEnterpriseOrgId("0");//0为未入驻企业
        }
        /*if(deptDate!=null){

        }else{

        }*/
        /*String no = enterpriseInfoMapper.getMaxEnterpriseNo(SecurityUtils.getOrgId().toString());
        no += "KH"+no;
        String newNo = "KH"+no;*/
        /*enterpriseInfo.setDevelopmentManager(getNickNameByName(enterpriseInfo.getDevelopmentManager()));
        enterpriseInfo.setBusinessManager(getNickNameByName(enterpriseInfo.getBusinessManager()));*/
       /* SysUser userDevelop = new SysUser();
        userDevelop.setNickName(enterpriseInfo.getDevelopmentManager());
        userDevelop.setBelongTo(SecurityUtils.getOrgId());
        SysUser userTransaction = new SysUser();
        userTransaction.setNickName(enterpriseInfo.getBusinessManager());
        userTransaction.setBelongTo(SecurityUtils.getOrgId());*/
        enterpriseInfo.setDevelopmentId(getUserIdByName(enterpriseInfo.getDevelopmentManager()));//开发经理
        enterpriseInfo.setTransactionId(getUserIdByName(enterpriseInfo.getBusinessManager()));//业务经理
        enterpriseInfo.setOrganizationId(SecurityUtils.getOrgId());
        enterpriseInfo.setOrganizationName(SecurityUtils.getOrgName());
        enterpriseInfo.setDepartmentId(SecurityUtils.getDeptId());
        enterpriseInfo.setDelFlag("0");
        enterpriseInfo.setIsShare("0");
        enterpriseInfo.setBelongTo(SecurityUtils.getOrgId().toString());
        enterpriseInfo.setDepartmentName(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
        //enterpriseInfo.setIdPath(mainTypeService.selectMainTypeById(enterpriseInfo.getClassificationId()).getIdPath());
       return count>0?enterpriseInfoMapper.updateEnterpriseInfo(enterpriseInfo):this.insertEnterpriseInfo(enterpriseInfo);
    }
    private String getNickNameByName(String name) {
        SysUser user = sysUserMapper.selectUserByUserName(name);
        if (user != null) {
            return user.getNickName();
        } else {
            return null;
        }
    }

    /**
     * 批量删除企业信息
     * 
     * @param enterpriseIds 需要删除的企业信息主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseInfoByEnterpriseIds(String[] enterpriseIds)
    {
        return deleteEnterpriseInfoByEnterpriseId(enterpriseIds[0]);
    }

    /**
     * 删除企业信息信息
     * 
     * @param enterpriseId 企业信息主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseInfoByEnterpriseId(String enterpriseId)
    {
        EnterpriseInfo enterpriseInfo=new EnterpriseInfo();
        enterpriseInfo.setEnterpriseId(enterpriseId);
        enterpriseInfo.setDelFlag("1");
        enterpriseInfo.setClassificationName("主分类");
        enterpriseInfo.setClassificationId("650c44e1-b575-4b62-9d54-1ba9d1e8abb7");
        return enterpriseInfoMapper.updateEnterpriseInfo(enterpriseInfo);
    }

    @Override
    public int editMainClass(BusinessMainClassVo businessMainClassVo) {
        //boolean flag = businessMainClassVo.getBusinessId().contains(",");
        int count = 0;
        /*
        EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
        List<EnterpriseInfo> enterpriseInfoList = selectEnterpriseInfoList(enterpriseInfo);

        for (EnterpriseInfo data : enterpriseInfoList) {
            EnterpriseInfo enterpriseInfoDate = new EnterpriseInfo();
            enterpriseInfoDate.setEnterpriseName(data.getEnterpriseName());
            List<EnterpriseInfo> dateNew = selectEnterpriseInfoByEnterpriseName(enterpriseInfoDate);
            if(dateNew.size()>0){
                data.setIsShare("1");
            }else{
                data.setIsShare("0");
            }
            count = enterpriseInfoMapper.updateEnterpriseInfo(data);
        }
        //用来快速修改 分辨旧数据是否已共享给平台 拿当前客户数据与企业来源为资源共享的数据匹配 匹配成功改为1 否则为0
        //用来快速修改 分辨旧数据是否为入驻企业 拿当前客户数据与组织机构表匹配查询
        EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
        SysDept dept =  new SysDept();
        List<EnterpriseInfo> enterpriseInfoList = enterpriseInfoMapper.selectEnterpriseInfoList(enterpriseInfo);
        for (EnterpriseInfo data : enterpriseInfoList) {
            EnterpriseInfo enterpriseInfoDate = new EnterpriseInfo();
            enterpriseInfoDate.setEnterpriseName(data.getEnterpriseName());
            //查是否共享
            List<EnterpriseInfo> dateNew = selectEnterpriseInfoByEnterpriseName(enterpriseInfoDate);
            data.setIsShare("0");
            enterpriseInfo.setEnterpriseOrgId("0");
            if(dateNew.size()>0){
                data.setIsShare("1");
            }
            dept.setDeptName(data.getEnterpriseName());
            dept.setIsCertified("2");
            SysDept deptDate = sysDeptMapper.selectDeptByDeptName(dept);
            if(deptDate!=null){
                data.setEnterpriseOrgId(deptDate.getDeptId().toString());
            }
            count = enterpriseInfoMapper.updateEnterpriseInfo(data);
        }*/
        /*if (flag) {
            String[] businessIds = businessMainClassVo.getBusinessId().split(",");
            for (int i = 0; i < businessIds.length; i++) {
                EnterpriseInfo info = enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseId(businessIds[i]);
                info.setClassificationId(businessMainClassVo.getMainClassId());
                info.setOperateTime(new Timestamp(new Date().getTime()));
                info.setOperateName(SecurityUtils.getUsername());
                info.setOperateId(SecurityUtils.getUserId());
                count = enterpriseInfoMapper.updateEnterpriseInfo(info);
            }
        } else {
            EnterpriseInfo info = enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseId(businessMainClassVo.getBusinessId());
            info.setClassificationId(businessMainClassVo.getMainClassId());
            info.setOperateTime(new Timestamp(new Date().getTime()));
            info.setOperateName(SecurityUtils.getUsername());
            info.setOperateId(SecurityUtils.getUserId());
            count =enterpriseInfoMapper.updateEnterpriseInfo(info);
        }*/
        return count;
    }

    @Override
    public int editBusinessManager(BusinessMainClassVo businessMainClassVo) {
        boolean flag = businessMainClassVo.getBusinessId().contains(",");
        int count = 0;
        if (flag) {
            String[] businessIds = businessMainClassVo.getBusinessId().split(",");
            for (int i = 0; i < businessIds.length; i++) {
                EnterpriseInfo info = enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseId(businessIds[i]);
                info.setTransactionId(businessMainClassVo.getTransactionId());
                info.setBusinessManager(businessMainClassVo.getBusinessManager());
                info.setOperateTime(new Timestamp(new Date().getTime()));
                info.setOperateName(SecurityUtils.getUsername());
                info.setOperateId(SecurityUtils.getUserId());
                count = enterpriseInfoMapper.updateEnterpriseInfo(info);
            }
        } else {
            EnterpriseInfo info = enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseId(businessMainClassVo.getBusinessId());
            info.setTransactionId(businessMainClassVo.getTransactionId());
            info.setBusinessManager(businessMainClassVo.getBusinessManager());
            info.setOperateTime(new Timestamp(new Date().getTime()));
            info.setOperateName(SecurityUtils.getUsername());
            info.setOperateId(SecurityUtils.getUserId());
            count =enterpriseInfoMapper.updateEnterpriseInfo(info);
        }
        return count;
    }

    @Override
    public List<EnterpriseInfo> getSupplierList(EnterpriseInfo enterpriseInfo) {
        enterpriseInfo.setBelongTo(SecurityUtils.getOrgId().toString());
      return   enterpriseInfoMapper.selectSupplierLis(enterpriseInfo);
    }

    @Override
    public int selectEnterpriseInfoNumber(String belongTo) {
        return enterpriseInfoMapper.selectEnterpriseInfoNumber(belongTo);
    }

    @Override
    public List<SysUser> getBusinessManagerList() {
        return sysUserMapper.getBusinessManagerList(SecurityUtils.getOrgId());
    }

    @Override
    public String getAddressById(String enterpriseId) {
        //先查询电销
        String address=electricSalesInfoMapper.selectEleAddressById(enterpriseId);
        if(Objects.isNull(address)){
          //说明电销为空，查询客户
            String enterpiseAddress=enterpriseInfoMapper.selectAddressById(enterpriseId);
            if(Objects.isNull(enterpiseAddress)){
                //说明客户也为空
                //查询公海
                HighSeas highSeas = highSeasMapper.selectHighSeasByEnterpriseId(enterpriseId);
                 return highSeas.getEnterpriseAddress();
            }else{
                return enterpiseAddress;
            }
        }else{
            return address;
        }
    }
}
